/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of CustomerStore.
	/// </summary>
	public class CustomerStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		public CustomerStore(Site site,HtmlWeb client)
		{
			_site = site;
			_client = client;
		}
		
		public List<IDictionary> GetCustomers()
		{
			List<IDictionary> Customers = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_customers&module=customers");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary Customer = (IDictionary)recs[i];
						Customer["address_books"] = GetCustomerAdresses(Customer["customers_id"].ToString());
						Customer["store_credits"] = GetCustomerCredits(Customer["customers_id"].ToString());
						Customer["wishlists"] = GetCustomerWishlists(Customer["customers_id"].ToString());
						Customers.Add(Customer);
					}									
				}
			}			
			
			return Customers;
		}
		
		private List<IDictionary> GetCustomerWishlists(string CustomerId)
		{
			List<IDictionary> Wishlists = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_wishlists&customers_id=" + CustomerId + "&module=customers");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary Wishlist = (IDictionary)recs[i];
						Wishlists.Add(Wishlist);												
					}									
				}
			}			
			
			return Wishlists;
		}
		
		private List<IDictionary> GetCustomerCredits(string CustomerId)
		{
			List<IDictionary> Credits = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_store_credits&customers_id=" + CustomerId + "&module=customers");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary Credit = (IDictionary)recs[i];
						Credits.Add(Credit);												
					}									
				}
			}			
			
			return Credits;
		}
		
		private List<IDictionary> GetCustomerAdresses(string CustomerId)
		{
			List<IDictionary> Adresses = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_address_books&customers_id=" + CustomerId + "&module=customers");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary Adresse = (IDictionary)recs[i];
						Adresses.Add(Adresse);												
					}									
				}
			}			
			
			return Adresses;
		}
	}
}
